<template>
  <div class="register">
    <div class="register-header"></div>
    <div class="formBox">
      <el-form
        ref="registerForm"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <img
              slot="prefix"
              src="@/assets/images/login/user.png"
              alt="dark"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleRegister"
          >
            <img
              slot="prefix"
              src="@/assets/images/login/password.png"
              alt="dark"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            auto-complete="off"
            placeholder="确认密码"
            @keyup.enter.native="handleRegister"
          >
            <img
              slot="prefix"
              src="@/assets/images/login/password.png"
              alt="dark"
              class="el-input__icon input-icon"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 63%"
            @keyup.enter.native="handleRegister"
          >
            <img
              slot="prefix"
              src="@/assets/images/login/code.png"
              alt="dark"
              class="el-input__icon input-icon"
            />
          </el-input>
          <div class="register-code">
            <img :src="codeUrl" @click="getCode" class="register-code-img" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            class="btn"
            @click.native.prevent="handleRegister"
          >
            <span v-if="!loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div style="float: right">
            <router-link class="link-type" :to="'/login'"
              >使用已有账户登录</router-link
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-register-footer">
      <img
        src="@/assets/images/login/logo.png"
        alt="dark"
        class="el-input__icon"
      />
      <div class="info">
        <p>技术支持：浙江弄潮儿智慧科技有限公司 支持电话：18667145678</p>
        <p>
          编译时间:{{ BUILD_DATE }}, 所在分支:{{ BRANCH }} 版本号:{{ VERSION }}
        </p>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      VERSION: process.env.VUE_APP_VERSION,
      BUILD_DATE: process.env.VUE_APP_BUILD_DATE,
      BRANCH: process.env.VUE_APP_BRANCH,
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: "",
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          {
            min: 2,
            max: 20,
            message: "用户账号长度必须介于 2 和 20 之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          {
            min: 5,
            max: 20,
            message: "用户密码长度必须介于 5 和 20 之间",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      captchaOnOff: true,
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          register(this.registerForm)
            .then((res) => {
              const username = this.registerForm.username;
              this.$alert(
                "<font color='red'>恭喜你，您的账号 " +
                  username +
                  " 注册成功！</font>",
                "系统提示",
                {
                  dangerouslyUseHTMLString: true,
                  type: "success",
                }
              )
                .then(() => {
                  this.$router.push("/register");
                })
                .catch(() => {});
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  // background-image: url("~@/assets/images/login/loginBg.png");
  background-attachment: fixed;
  background-size: cover;
  overflow: hidden;
  .register-header {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 66px;
    overflow: hidden;
    // background-image: url("~@/assets/images/login/headerBg.png");
    background-repeat: no-repeat;
    background-size: cover;
  }
}
.formBox {
  width: 400px;
  height: 400px;
  position: absolute;
  right: 20%;
  margin-top: -100px;
  padding: 40px;
  // background-image: url("~@/assets/images/login/formBg.png");
  background-repeat: no-repeat;
  background-size: cover;
  .register-form {
    border-radius: 6px;
    .el-input {
      height: 38px;
      ::v-deep input {
        height: 38px;
        background: #000000;
        border-color: #30aaff;
        padding-left: 35px;
      }
    }
    .input-icon {
      height: 22px;
      width: 22px;
      margin-left: 2px;
      vertical-align: middle;
    }
    .btn {
      width: 100%;
      height: 38px;
      color: #000000;
      border: none;
      background: linear-gradient(180deg, #00f9ff 0%, #00f1fd 100%);
    }
    .register-code {
      width: 33%;
      height: 38px;
      float: right;
      img {
        cursor: pointer;
        vertical-align: middle;
      }
    }
    .register-code-img {
      height: 38px;
    }
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 10px;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-input__icon {
    vertical-align: middle;
    margin-right: 10px;
  }
  .info {
    p {
      margin: 5px 0;
      line-height: initial;
    }
  }
}
.register-code-img {
  height: 38px;
}
</style>
